<template>
  <div>
    <el-container>
      <!-- 头部区 -->
      <el-header>
        <div>
          <img src="@/assets/login.jpg" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="isCollapse =! isCollapse">|||</div>
          <el-menu background-color="rgb(94, 94, 94)" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active='activePath'>
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="`/${subitem.path}`" v-for="subitem in item.children" :key="subitem.id" @click="saveActivePath(`/${subitem.path}`)">
                <!-- 二级菜单模板区 -->
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subitem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体区 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'MyHome',
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getmenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getmenuList() {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('res.meta.msg')
      this.menuList = res.data
    },
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang='less' scoped>
#app > div {
  height: 100%;
}
.el-container {
  height: 100%;
}
.el-header {
  height: 70px !important;
  background-color: rgb(54, 54, 54);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
  }
  img {
    width: 60px;
    border-radius: 50%;
  }
  div {
    display: flex;
    align-items: center;
  }
  img {
    width: 60px;
    border-radius: 50%;
  }
  span {
    margin: 0 10px;
  }
}
.el-aside {
  background-color: rgb(195, 195, 195);
  background-color: rgb(94, 94, 94);
  .el-menu {
    border-right: 0;
  }
  .iconfont {
    margin-right: 10px;
  }
  .toggle-button {
    background-color: rgb(66, 66, 66);
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.5em;
    cursor: pointer;
  }
  .el-menu-item.is-active {
    color: #409eff !important;
  }
}
.el-main {
  background-color: rgb(245, 245, 245);
}
</style>
